﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCB.UI.PopWindows;

namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 汇总年计划主表
    /// </summary>
    /// Title:ZMCB40
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 梁琦
    /// Version: 1.0
    /// History:
    ///       2013-7-07  梁琦   [创建]
    ///  
    ///
    public partial class ZMCB40 : EFViewCmes
    {
        private Dictionary<object, string> TabToPageName; //存放页签与页面号的对应关系
        String year;
        String status;
        String ForeignId;
        String Editor;
        EIInfo eiInfo2;

        public ZMCB40()
        {
            InitializeComponent();
            TabToPageName = new Dictionary<object, string>();
            base.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCB40_LayoutUpdated);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            year = (string)initData.Get("year");
            status = (string)initData.Get("status");
            ForeignId = (string)initData.Get("ForeignId");
            Editor = (string)initData.Get("Editor");
            this.titleText.Text = year + "年生产计划";
            this.StatusText.Text = "计划状态：" + status;
            this.fdYear.Value = year;

            this.fdMineTarget.Value = (string)initData.Get("fdMineTarget");
            this.fdGrubTarget.Value = (string)initData.Get("fdGrubTarget");
            this.fdDeploitationTarget.Value = (string)initData.Get("fdDeploitationTarget");
            this.fdGrubCoalTarget.Value = (string)initData.Get("fdGrubCoalTarget");

            base.Initialize(initData);
            eiInfo2 = initData;
        }

        /// <summary>
        /// 根据主表的状态设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCB40_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.status == null)
            {
                return;
            }
            if (!(this.status.Equals("草拟") || this.status.Equals("无计划")))
            {
                Button insert = this.FindName("INSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button update = this.FindName("UPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = false;
                }
                Button effect = this.FindName("EFFECT") as Button;
                if (effect != null)
                {
                    effect.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryButtonClick(object sender, RoutedEventArgs e)
        {

            Submit(OperationMode.Query, "ZMCB40", "query", DataRange.All, queryCompleted);

        }
        private void queryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            year = (string)e.EIInfo.Get("year");
            status = (string)e.EIInfo.Get("status");
            ForeignId = (string)e.EIInfo.Get("ForeignId");
            Editor = (string)e.EIInfo.Get("Editor");
            eiInfo2 = e.EIInfo;

            this.fdMineTarget.Value = (string)eiInfo2.Get("fdMineTarget");
            this.fdGrubTarget.Value = (string)eiInfo2.Get("fdGrubTarget");
            this.fdDeploitationTarget.Value = (string)eiInfo2.Get("fdDeploitationTarget");
            this.fdGrubCoalTarget.Value = (string)eiInfo2.Get("fdGrubCoalTarget");
            
            this.titleText.Text = year + "年生产计划";
            this.StatusText.Text = "计划状态：" + status;
            RefreshData();

            if (!(status.Equals("草拟") || status.Equals("无计划")))
            {
                Button insert = this.FindName("INSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button update = this.FindName("UPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = false;
                }
                Button effect = this.FindName("EFFECT") as Button;
                if (effect != null)
                {
                    effect.IsEnabled = false;
                }
            }
            else 
            {
                Button insert = this.FindName("INSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = true;
                }
                Button update = this.FindName("UPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = true;
                }
                Button effect = this.FindName("EFFECT") as Button;
                if (effect != null)
                {
                    effect.IsEnabled = true;
                }
            }
        }

        /// <summary>
        /// 页签改变加载不同的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tab_SelectionChanged(object sender, RoutedEventArgs e)
        {
           RefreshData();
        }


        /// <summary>
        /// 刷新页签页面
        /// </summary>
        private void RefreshData()
        {
            if (TabSource.SelectedItem != null)
            {
                EFTabItem item = TabSource.SelectedItem as EFTabItem;
                item.Content = null;

                if (item.Name.Equals("tab1"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdForeignId", ForeignId);
                    args.Add("fdStatus", status);
                    args.Add("fdYear", year);
                    item.LoadView("ZMCB43", args);
                }
                else if (item.Name.Equals("tab2"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdForeignId", ForeignId);
                    args.Add("fdStatus", status);
                    args.Add("fdYear", year);
                    item.LoadView("ZMCB41", args);
                }
                else if (item.Name.Equals("tab3"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdForeignId", ForeignId);
                    args.Add("fdStatus", status);
                    args.Add("fdYear", year);
                    item.LoadView("ZMCB42", args);
                }
                else if (item.Name.Equals("tab4"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdForeignId", ForeignId);
                    args.Add("fdStatus", status);
                    args.Add("fdYear", year);
                    item.LoadView("ZMCB51", args);
                }
            }
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "生产年计划-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            this.TempButton = sender as Button;
            this.TempButton.IsEnabled = false;

            EIInfo eiInfo = new EIInfo();
            eiInfo = eiInfo2;
            OpenPopup(eiInfo, "生产年计划-修改", "update", null);
            
        }

       

        /// <summary>
        /// 生效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnEffectButtonClick(object sender, RoutedEventArgs e)
        {

            this.TempButton = sender as Button;
            this.TempButton.IsEnabled = false;

            
                DialogParameters param = new DialogParameters();
                param.CancelButtonContent = "取消";
                param.OkButtonContent = "确定";
                param.Content = "确定生效该年计划吗?";
                param.Header = "确认框";
                param.Closed = WindowAlertClosed;
                EFWindow.Confirm(param);
            
        }

        private void WindowAlertClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，删除按钮生效
                return;
            }
            EIInfo info = new EIInfo();
            info = eiInfo2;
            info.Set(EIConstants.ServiceNameKey, "ZMCB40");
            info.Set(EIConstants.MethodNameKey, "effect");

            this.Submit(info, effectCompleted);
        }

        /// <summary>
        //点击保存后的回调函数
        /// </summary>
        private void effectCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("缺少采煤计划或掘进计划，不能生效");
                base.TempButtonOpen(true);
                return;
            }
            EFWindow.Alert("生效成功！");
            base.TempButtonOpen(true);
            Submit(OperationMode.Query, "ZMCB40", "query", DataRange.All, queryCompleted);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                // this.grid.Refresh();
                Submit(OperationMode.Query, "ZMCB40", "query", DataRange.All, queryCompleted);
            }

            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            S40AddYearPlan view = new S40AddYearPlan(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();

            view.EIDataContext.Update(_eIInfo);
            if (_dialogType.Equals("insert"))
            {
                view.fdYear.Value = year;
                view.fdEditor.Value = Editor;
                view.fdName.Value = year + "年生产计划";
                view.fdEditDate.Value=DateTime.Now;
                
            }
            if (_dialogType.Equals("update"))
            {
                view.fdYear.IsReadOnly = true;
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 重新基类的方法，当有新的EIInfo 数据发布时回调的方法
        /// 生产进度新增修改后更新统计数据
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            //刷新页面
            if (this.StatusText.Text == "计划状态：无计划")
            {
                Submit(OperationMode.Query, "ZMCB40", "query", DataRange.All, queryCompleted);
            }
        }

    }
}
